package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidPrintTemplate;
import com.apache.bid.manager.BidPrintTemplateManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class BidPrintTemplateManagerImpl implements BidPrintTemplateManager {

	protected IDao bidPrintTemplateDao;
	protected final String entityName = "com.apache.bid.entity.BidPrintTemplate";

	public void setBidPrintTemplateDao(IDao bidPrintTemplateDao) {
		this.bidPrintTemplateDao = bidPrintTemplateDao;
	}

	public String saveInfo(ParamsVo<BidPrintTemplate> vo) throws BusinessException {
		BidPrintTemplate bidPrintTemplate = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidPrintTemplate_"+infoId;
		bidPrintTemplate.setTemplateId(infoId);
		MethodParam param = new MethodParam("BidPrintTemplate", cacheKey, "", entityName);
		param.setVaule(bidPrintTemplate);
		if (bidPrintTemplateDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidPrintTemplate> vo) throws BusinessException {
		BidPrintTemplate bidPrintTemplate = vo.getObj();
		if (Validator.isNotNull(bidPrintTemplate.getTemplateId())) {
			String cacheKey = "";
			//String cacheKey="bidPrintTemplate_"+bidPrintTemplate.getTemplateId();
			MethodParam param = new MethodParam("BidPrintTemplate", cacheKey, "", entityName);
			param.setVaule(bidPrintTemplate);
			return bidPrintTemplateDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidPrintTemplate> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidPrintTemplate_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidPrintTemplate info = (BidPrintTemplate) bidPrintTemplateDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidPrintTemplate");
			return bidPrintTemplateDao.edit(param);
		} else {
			param.setParams("templateId", infoId);
			param.setDelete(true);
			return bidPrintTemplateDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidPrintTemplate> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidPrintTemplate_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidPrintTemplateDao.selectById(param);
	}

	public Object execute(ParamsVo<BidPrintTemplate> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidPrintTemplate> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = bidPrintTemplateDao.pageSelect(param);
		return page;
	}

	public List<BidPrintTemplate> getList(ParamsVo<BidPrintTemplate> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return bidPrintTemplateDao.select(param);
	}

	public long countInfo(ParamsVo<BidPrintTemplate> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidPrintTemplateDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidPrintTemplate> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidPrintTemplateDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidPrintTemplate bidPrintTemplate = vo.getObj();
		param.setResultCache(false);
		if (Validator.isNotNull(bidPrintTemplate.getOrgId())) {
			sb.append(" and orgId = :orgId");
			param.setParams("orgId", bidPrintTemplate.getOrgId());
		}
		if (Validator.isNotNull(bidPrintTemplate.getOrgCname())) {
			sb.append(" and orgCname = :orgCname");
			param.setParams("orgCname", bidPrintTemplate.getOrgCname());
		}
		if (Validator.isNotNull(bidPrintTemplate.getTemplateName())) {
			sb.append(" and templateName  like '%" + bidPrintTemplate.getTemplateName() + "%' ");
			param.setParams("templateName", " like '%" + bidPrintTemplate.getTemplateName() + "%' ");
		}
		if (Validator.isNotNull(bidPrintTemplate.getTemplatePath())) {
			sb.append(" and templatePath = :templatePath");
			param.setParams("templatePath", bidPrintTemplate.getTemplatePath());
		}
		if (Validator.isNotNull(bidPrintTemplate.getTemplateDesc())) {
			sb.append(" and templateDesc = :templateDesc");
			param.setParams("templateDesc", bidPrintTemplate.getTemplateDesc());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
